#include "aliot_dm.h"
#include <stdio.h>
#include <stdlib.h>
//生成推送属性主题
int32_t dm_property_post(char** dest,char *product_key, char *device_name)
{
	int totalSize = strlen("/sys///thing/event/property/post") + strlen(product_key) + strlen(device_name) + 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/sys/%s/%s/thing/event/property/post",product_key,device_name);
    return totalSize - 1;
}

//生成推送属性回复主题
int32_t dm_property_post_reply(char** dest,char *product_key, char *device_name)
{
	int totalSize = strlen("/sys///thing/event/property/post_reply") + strlen(product_key) + strlen(device_name) + 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/sys/%s/%s/thing/event/property/post_reply",product_key,device_name);
    return totalSize - 1;
}

//生成推送事件上报主题
int32_t dm_event_post(char** dest,char *product_key, char *device_name,char* event_id)
{
	int totalSize = strlen("/sys///thing/event//post") + strlen(product_key) + strlen(device_name) + strlen(event_id)+ 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/sys/%s/%s/thing/event/%s/post",product_key,device_name,event_id);
    return totalSize - 1;
}

//生成推送事件上报回复主题
int32_t dm_event_post_reply(char** dest,char *product_key, char *device_name,char* event_id)
{
	int totalSize = strlen("/sys///thing/event//post_reply") + strlen(product_key) + strlen(device_name) + strlen(event_id)+ 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/sys/%s/%s/thing/event/%s/post_reply",product_key,device_name,event_id);
    return totalSize - 1;
}

//生成属性上报JSON字符串，返回值为字符串长度
int32_t dm_property_json(char** dest,int id,cJSON* paramObj)
{
    cJSON * root =  cJSON_CreateObject();
    char s_id[9];
    snprintf(s_id,9,"%d",id);
    cJSON_AddStringToObject(root, "id", s_id);
    cJSON_AddStringToObject(root, "version", "1.0");
    cJSON_AddStringToObject(root, "method", "thing.event.property.post");
    cJSON_AddItemToObject(root, "params", paramObj);
    
    *dest = cJSON_Print(root);
    cJSON_Delete(root);
    return strlen(*dest);
}
